import os, random, time

userinfo_path = os.path.abspath('..') + '\\data\\userinfo.txt'

# 对输入的字符串进行判断，确认其是否可以转换为一个有效的数字
def check_number(string):
    point = 0
    is_valid = True
    for c in string:
        code = ord(c)
        if (code < 48 and code != 46) or code > 57:
            is_valid = False
            break
        elif code == 46: point += 1

    if point > 1: is_valid = False

    return is_valid

# 检查电话号码
def check_phone(phone):
    if check_number(phone):
        if len(phone) != 11:
            return False
        else:
            if phone[0] != '1':
                return False
            elif phone[1] != '3' and phone[1] != '4' and phone[1] != '5' and phone[1] != '7' and phone[1] != '8' and phone[1] != '9':
                return False
    else:
        return False

    return True


# 读取用户信息文件，并返回为一个列表对象
def read_user():
    user_file = open(userinfo_path)
    user_list = user_file.readlines()
    return user_list


# 往userinfo.txt文件追加一条用户信息
def write_userinfo(content):
    user_file = open(userinfo_path, mode='a+')
    user_file.write(content)
    user_file.close()